package vn.bus.bk;

import java.util.ArrayList;

import vn.bus.bk.adapter.GridMenuAdapter;
import vn.bus.bk.dialog.DialogChooseCity;
import vn.bus.bk.dialog.DialogInfo;
import vn.bus.bk.help.HelpInfo;
import vn.bus.bk.object.ItemObject;
import vn.bus.bk.place.SearchToPlace;

import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Menu extends Activity implements OnItemClickListener {

    private GridView gridViewItem;
    private ArrayList<ItemObject> listObject;
    private GridMenuAdapter myAdapter;
    private Display display;
    public static final String FAVORITE = "favorite";
    private static final int INFO = android.view.Menu.FIRST;
    private static final int CHOOSE_CITY = android.view.Menu.FIRST + 1;
    private static final int EXIT = android.view.Menu.FIRST + 3;
    private static final int SEND_MAIL = android.view.Menu.FIRST + 2;
//    private AdView adView;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.menu);
	
//	adView = (AdView) this.findViewById(R.id.adView);
//	
//	if(adView != null){
//	    Log.i("state", "Load ad");
//	    adView.loadAd(new AdRequest());
//	}

	display = getWindowManager().getDefaultDisplay();
	init();
	createData();
	myAdapter = new GridMenuAdapter(this, listObject, display.getWidth(),
		display.getHeight());
	gridViewItem.setAdapter(myAdapter);
	gridViewItem.setOnItemClickListener(this);

    }

    private void init() {
	// TODO Auto-generated method stub
	gridViewItem = (GridView) findViewById(R.id.gridItems);
    }

    private void createData() {
	// TODO Auto-generated method stub
	listObject = new ArrayList<ItemObject>();
	listObject.add(new ItemObject(
		createBitmapFromId(R.drawable.ic_number_bus),
		getString(R.string.menu_item_1)));
	listObject.add(new ItemObject(createBitmapFromId(R.drawable.ic_search),
		getString(R.string.menu_item_2)));
	listObject.add(new ItemObject(
		createBitmapFromId(R.drawable.ic_search_route),
		getString(R.string.menu_item_3)));
	listObject.add(new ItemObject(
		createBitmapFromId(R.drawable.ic_advance),
		getString(R.string.menu_item_4)));
	listObject.add(new ItemObject(createBitmapFromId(R.drawable.reg),
		getString(R.string.menu_item_5)));
	listObject.add(new ItemObject(createBitmapFromId(R.drawable.location),
		getString(R.string.menu_item_6)));
	listObject.add(new ItemObject(
		createBitmapFromId(R.drawable.ic_favorite),
		getString(R.string.menu_item_7)));

    }

    public Bitmap createBitmapFromId(int _id) {

	return BitmapFactory.decodeResource(getResources(), _id);
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
	    long arg3) {
	// TODO Auto-generated method stub
	switch (position) {
	case 0:
	    startActivity(new Intent(this, SearchByNumber.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	case 1:
	    startActivity(new Intent(this, SearchByPlace.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	case 2:
	    startActivity(new Intent(this, SearchByTwoPlace.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	case 3:
	    startActivity(new Intent(this, SearchToPlace.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	case 4:
	    startActivity(new Intent(this, HelpInfo.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	case 5:
	    startActivity(new Intent(this, MyLocation.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	default:
	    startActivity(new Intent(this, Favorite.class));
	    overridePendingTransition(R.anim.fade, R.anim.hold);
	    break;
	}
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
	// TODO Auto-generated method stub
	menu.add(0, INFO, 0, getResources().getString(R.string.info_app_title))
		.setIcon(R.drawable.info_app);
	menu.add(0, CHOOSE_CITY, 0, getString(R.string.choose_city)).setIcon(
		R.drawable.choose_city);
	menu.add(0, SEND_MAIL, 0, getString(R.string.support_menu)).setIcon(
		R.drawable.support);
	menu.add(0, EXIT, 0, getResources().getString(R.string.exit)).setIcon(
		R.drawable.quit_app);

	return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	// TODO Auto-generated method stub
	switch (item.getItemId()) {
	case INFO:
	    DialogInfo dialoginfo = new DialogInfo(this);
	    dialoginfo.show();
	    break;
	case EXIT:
	    finish();
	case CHOOSE_CITY:
	    DialogChooseCity dialog = new DialogChooseCity(this);
	    dialog.show();
	    break;
	case SEND_MAIL:
	    sendMail();
	    break;
	}
	return super.onOptionsItemSelected(item);
    }

    private void sendMail() {
	Intent i = new Intent(Intent.ACTION_SEND);
	i.setType("message/rfc822");
	i.putExtra(Intent.EXTRA_EMAIL, new String[] { "chungtb21@gmail.com",
		"magicmaster.no1@gmail.com" });
	i.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.msg_subject));
	i.putExtra(Intent.EXTRA_TEXT, "");
	try {
	    startActivity(Intent.createChooser(i, "Send mail..."));
	} catch (android.content.ActivityNotFoundException ex) {
	    Toast.makeText(Menu.this, "There are no email clients installed.",
		    Toast.LENGTH_SHORT).show();
	}
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	// TODO Auto-generated method stub
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setTitle(getString(R.string.announce));
	    builder.setMessage(getString(R.string.request_exit));
	    builder.setPositiveButton(getString(R.string.yes),
		    new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface arg0, int arg1) {
			    // TODO Auto-generated method stub
			    finish();
			}
		    });

	    builder.setNegativeButton(getString(R.string.no),
		    new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface arg0, int arg1) {
			    // TODO Auto-generated method stub

			}
		    });
	    builder.show();
	    return true;
	}
	return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
//	if (adView != null) {
//	    adView.destroy();
//	}
	super.onDestroy();
    }

}
